#!/bin/bash
commit_msg=$(cat "$1")
# 如果提交信息包含 'Merge branch' 或 'Merge pull request'，则跳过 commitlint 检查
if echo "$commit_msg" | grep -qE '^Merge (branch|remote|pull request)'; then
  exit 0
fi

if [ "$2" = "merge" ] || [ "$2" = "squash" ]; then
  exit 0
fi

commit_msg=$(cat "$1")
npx --no -- commitlint --edit "$1" >/dev/null || {

  arr=(
    "(1)feat: 新增用户登录功能 #议题Id1#议题Id2;   "
    "(2)fix: 修复登录跳转错误 #议题Id1#议题Id2;   "
    "(3)docs: 更新项目文档 #议题Id1#议题Id2;  "
    "(4)chore: 升级npm依赖库 #议题Id1#议题Id2; "
    "(5)perf: 优化页面加载速度 #议题Id1#议题Id2; "
    "(6)refactor: 用户登录重构 #议题Id1#议题Id2;"
    "(7)style: 用户登录样式调整 #议题Id1#议题Id2; "
    "(8)test: 用户登录测试 #议题Id1#议题Id2; "
    "(9)revert: 撤销用户登录提交 #议题Id1#议题Id2;"
  )

  joined=$(IFS=';' echo "${arr[*]}")
  echo "提交信息 '$commit_msg' 不符合规范，请使用规范的格式提交！正确示例：${joined}"
  exit 1
}

issues=$(echo "$commit_msg" | grep -oE '#[0-9]+' | sort -u | tr -d '\n')

chmod +x "$(dirname "$0")/check-issue"
echo "commit_msg: $issues"
"$(dirname "$0")/check-issue" "$issues"
exit $?
